//
//  QMySelfVC.m
//  QoilPublic
//
//  Created by zhuang on 16/7/4.
//  Copyright © 2016年 杨凯. All rights reserved.
//

#import "QMySelfVC.h"
#import "NewMySelfHeadCell.h"
#import "QMoneyCell.h"
#import "MySelfItemsCell.h"
#import "QLoginVC.h"
#import "QMySelfInformationVC.h"
#import "QAllOrderMoreVC.h"
#import "QMyCouponsVC.h"
#import "QTopupMoneyVC.h"
#import "QFeedBackVC.h"
#import "QBillingDetailVC.h"
#import "QDTProtocolVC.h"
#import "QWriteDataVC.h"
#import "QMyIncomeVC.h"
#import "QInvoiceManageVC.h"
#import "QCouponVC.h"
#import "QPointsVC.h"
#import "QActivityWebVC.h"
#import "AboutVC.h"
#import "CloudBoxVC.h"
#import "QLoginVC.h"
#import "DiTuiCell.h"
const CGFloat headHeight = 265;
@interface QMySelfVC ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tbView;
@property (nonatomic, strong) UIImageView *bacImageView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@end

@implementation QMySelfVC{

    QLoginModel *_loginModel;
}

- (void)dealloc{

    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    [self.navigationController  setNavigationBarHidden:YES animated:animated];
    
}
- (void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
//- (void)viewWillAppear:(BOOL)animated{
//    [super viewWillAppear:animated];
//    //半透明
//    [self.navigationController setNavigationBarHidden:YES animated:animated];
//     [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];//设置状态栏
//    [self setStatusBarBackgroundColor:[UIColor whiteColor]];
//
//}
////设置状态栏颜色
//- (void)setStatusBarBackgroundColor:(UIColor *)color {
//
//    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
//    if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
//        statusBar.backgroundColor = color;
//    }
//}
//
//- (void)viewWillDisappear:(BOOL)animated{
//
//    [super viewWillDisappear:animated];
//
//    [self.navigationController setNavigationBarHidden:NO animated:animated];
//    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];//设置状态栏
//    [self setStatusBarBackgroundColor:[UIColor clearColor]];
//
//}
- (void)tabBarRequest1{
    
    [self.dataSource removeAllObjects];
    [self.tbView reloadData];
}


- (void)tabBarRequest{
    
        [self requestData];
}

- (void)viewDidLoad {

    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self requestData];
    [self createUI];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginStateDidsignup) name:kNotificationLoginSignup object:nil];//登录的通知
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginStateDidsignout) name:kNotificationLoginSignOut object:nil];//退出登录的通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(loginStateDidsignup) name:kNotificationTopUpMoneySuccess object:nil];//充值成功
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tabBarRequest) name:kNotificationTabBar4 object:nil];
    
}

//登录的通知
- (void)loginStateDidsignup{//重新请求数据
    [self requestData];
}

//退出登录的通知
- (void)loginStateDidsignout{
    _loginModel = nil;
    self.tabBarController.selectedIndex = 0;
    
}
- (UITableView *)tbView{
    
    if (!_tbView) {
        _tbView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tbView.delegate = self;
        _tbView.dataSource = self;
        _tbView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        _tbView.separatorStyle = UITableViewCellSelectionStyleNone;

        if (@available(iOS 11.0, *)) {
            
            self.tbView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        
        } else {
            
            self.automaticallyAdjustsScrollViewInsets = NO;
        
        }
        
        
    }
    
    return _tbView;
    
}
- (void)createUI{
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tbView];
    [self.tbView registerNib:[UINib nibWithNibName:@"QMySelfHeadCell" bundle:nil] forCellReuseIdentifier:@"QMySelfHeadCell"];
    [self.tbView registerNib:[UINib nibWithNibName:@"QMoneyCell" bundle:nil] forCellReuseIdentifier:@"QMoneyCell"];
    [self.tbView registerNib:[UINib nibWithNibName:@"MySelfItemsCell" bundle:nil] forCellReuseIdentifier:@"MySelfItemsCell"];
    [self.tbView registerNib:[UINib nibWithNibName:@"NewMySelfHeadCell" bundle:nil] forCellReuseIdentifier:@"NewMySelfHeadCell"];
    [self.tbView registerNib:[UINib nibWithNibName:@"DiTuiCell" bundle:nil] forCellReuseIdentifier:@"DiTuiCell"];
}

#pragma mark --- tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0||section == 1) {
        return 1;
    }else if (section == 2){
          return self.dataSource.count;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    __weak QMySelfVC *wself = self;
    if (indexPath.section == 0) {
        NewMySelfHeadCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewMySelfHeadCell" forIndexPath:indexPath];
        cell.loginModel = _loginModel;
                cell.headBlock = ^(){
                     __strong QMySelfVC *sSelf = wself;
                    CLog(@"点击头像");
                    if([QUserInfo sharedInfo].loginModel){
                        QMySelfInformationVC *vc = [[QMySelfInformationVC alloc] init];
                        [sSelf.navigationController pushViewController:vc animated:YES];
                    }else{
                        QLoginVC *vc = [[QLoginVC alloc] init];
                        [sSelf.navigationController pushViewController:vc animated:YES];
                    }
                };
        
                cell.dtBlock = ^(){
                    __strong QMySelfVC *sSelf = wself;
        
                    if(![QUserInfo sharedInfo].loginModel){
                        QLoginVC * vc = [[QLoginVC alloc] init];
                        [sSelf.navigationController pushViewController:vc animated:YES];
                    }else{
        
                        //0 改为99
                        if([_loginModel.marketingStatus isEqualToString:@"0"]){
                            //我的兼职收益---杨凯
                            QMyIncomeVC * vc = [[QMyIncomeVC alloc]init];
                            [self.navigationController pushViewController:vc animated:YES];
        
                        }else if ([_loginModel.marketingStatus isEqualToString:@"1"]){
                        }else if ([_loginModel.marketingStatus isEqualToString:@"2"]){
                        }else if ([_loginModel.marketingStatus isEqualToString:@"3"]){
                        }else if ([_loginModel.marketingStatus isEqualToString:@"4"]){
                        }else if ([_loginModel.marketingStatus isEqualToString:@"99"]){
        
                        QWriteDataVC *vc = [[QWriteDataVC alloc] init];
                        [self.navigationController pushViewController:vc animated:YES];
                       
                        }
                 
                    }
              
                };
        
        return cell;
    }

    else if(indexPath.section == 1){
        
        DiTuiCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DiTuiCell"];
        cell.loginModel = _loginModel;
        return cell;
        
    }else if (indexPath.section == 2){
        
        MySelfItemsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MySelfItemsCell" forIndexPath:indexPath];
        NSString *title = [self.dataSource objectAtIndex:indexPath.row];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        cell.titleLabel.text = title;
        
        cell.iconImage.image = [UIImage imageNamed:title];
        if ([title isEqualToString:@"当前版本"]) {
            cell.hidden = YES;
        }if ([title isEqualToString:@"提现"]) {
            cell.hidden = YES;
        }
        
        return cell;
    }
        
       
    return nil;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if(indexPath.section == 0){
        return headHeight;
    }else  if (indexPath.section == 1) {
        
        return 60;
    }else if (indexPath.section == 2) {
        
        return 50;
    }
    return 0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    if (indexPath.section == 0) {
        
        return;
        
    }
    if (indexPath.section ==  1) {
        
        if(![QUserInfo sharedInfo].loginModel){
            QLoginVC * vc = [[QLoginVC alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            
            //0 改为99
            if([_loginModel.marketingStatus isEqualToString:@"0"]){
                //我的兼职收益---杨凯
                QMyIncomeVC * vc = [[QMyIncomeVC alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
                
            }else if ([_loginModel.marketingStatus isEqualToString:@"1"]){
            }else if ([_loginModel.marketingStatus isEqualToString:@"2"]){
            }else if ([_loginModel.marketingStatus isEqualToString:@"3"]){
            }else if ([_loginModel.marketingStatus isEqualToString:@"4"]){
            }else if ([_loginModel.marketingStatus isEqualToString:@"99"]){
                
                QWriteDataVC *vc = [[QWriteDataVC alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
            }
        }
        
        
    }
    if (indexPath.section == 2) {
        MySelfItemsCell *cell = [self.tbView cellForRowAtIndexPath:indexPath];
        NSString *title = @"";
        if (!([cell.titleLabel.text isEqualToString:@""]||cell.titleLabel.text == nil)) {
            
            title = cell.titleLabel.text;
        }
        
        if([QUserInfo sharedInfo].loginModel){
            
            if ([title isEqualToString:@"我的优惠券"]){
                CLog(@"我的优惠券");
                QCouponVC * vc = [[QCouponVC alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
            }else if ([title isEqualToString:@"我的积分"]){
                QPointsVC * vc = [[QPointsVC alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
            }else if ([title isEqualToString:@"个人设备管理"]){
                CLog(@"个人设备管理");
                QActivityWebVC *vc = [[QActivityWebVC alloc] init];
                vc.url = [NSString stringWithFormat:@"http://www.ydaol.com/oilWX_api/client/EquipmentList.html?token=%@",[QUserInfo sharedInfo].loginModel.token];
                //            vc.url = [NSString stringWithFormat:@"http://wx.ydaol.com/oilWX_api/client/EquipmentList.html?token=%@",[QUserInfo sharedInfo].loginModel.token];
                [self.navigationController pushViewController:vc animated:YES];
            }else if ([title isEqualToString:@"发票管理"]){
                CLog(@"发票管理");
                QInvoiceManageVC * vc =[[QInvoiceManageVC alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
            }else if ([title isEqualToString:@"账单明细"]){
                CLog(@"账单明细");
                QBillingDetailVC *vc = [[QBillingDetailVC alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
                
            }else if ([title isEqualToString:@"意见反馈"]){
                QFeedBackVC *vc = [[QFeedBackVC alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
            }else if ([title isEqualToString:@"关于我们"]){
                AboutVC *about = [[AboutVC alloc]init];
                [self.navigationController pushViewController:about animated:YES];
            }
//            else if ([title isEqualToString:@"我的云盒"]){
//                CloudBoxVC *vc = [[CloudBoxVC alloc]init];
//                [self.navigationController pushViewController:vc animated:YES];
//            }
        } else{
            [QViewUtility showAlertChooseViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"您还未登录,请登录" leftTitle:@"取消" rightTitle:@"立即登录" type:1 myBlock:^(NSInteger tag) {
                if(tag == 1){
                    QLoginVC *vc = [[QLoginVC alloc] init];
                    [self.navigationController pushViewController:vc animated:YES];
                }
            }];
        }
    }
}

#pragma mark - Getter/Setter


#pragma mark - 用户个人中心
- (void)requestData{
    
    
    
    NSString *url = [QParamService urlWithAction:@"myCentre"];
    NSDictionary *param = [QParamService getMyCentreData:@"2"];
    [QViewUtility showMBHudToView:self.view hideBg:NO];
    [[QHttpManager sharedManager] POST:url parameterMD5:param completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [QViewUtility hideMBHudToView:self.view];
        if(error){
            [_tbView reloadData];
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
            _loginModel = [QLoginModel objectFromDict:basModel.items];
            [QUserInfo sharedInfo].loginModel = _loginModel;
            [QDataUtility updateLoginModel:[QUserInfo sharedInfo].loginModel];
            [self testRequestData];
        }else{
//           [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
            if([basModel.errMsg isEqualToString:@"请重新登录"]||[basModel.errMsg isEqualToString:@"参数格式错误或者不合法"]){
                QLoginVC *lonin = [[QLoginVC alloc]init];
                lonin.LoginAnimationStyle = LoginAnimationStyle_mySelf;
                [self.navigationController pushViewController:lonin animated:YES];
                [QDataUtility deleteLoginModel];
            }
        }
        
    }];
}
-(NSMutableArray *)dataSource
{
    if (!_dataSource) {
        _dataSource = [NSMutableArray new];
    }
    return _dataSource;
}

//获取显示的cell内容
- (void)testRequestData{
    NSString *url = [QParamService urlWithAction:@"usercard/getMenuConfig"];
    NSDictionary *param = [QParamService getUsercard];
    
    [QViewUtility showMBHudToView:self.view hideBg:NO];
    [[QHttpManager sharedManager] POST:url parameterMD5:param completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [QViewUtility hideMBHudToView:self.view];
        if(error){
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
             [self.dataSource addObject:@"兼职地推"];
            NSDictionary *tmpDic = [dict objectForKey:@"items"];
            
            NSLog(@"tmpDic:%@",tmpDic);
            
            NSArray *array = [tmpDic objectForKey:@"list"];
            NSMutableArray  *bottomArray = [NSMutableArray new];
            if (array.count>0) {
                for (NSDictionary *tmp in array) {
                    NSString *type = [tmp objectForKey:@"type"];
                    NSString *isDisplay = [tmp objectForKey:@"isDisplay"];
                    if ([type isEqualToString:@"2"]&&[isDisplay isEqualToString:@"0"]) {
                        [bottomArray addObject:tmp];
                    }
                }
            }
            [self.dataSource removeAllObjects];
            for (NSDictionary *tmpDic in bottomArray) {
                NSLog(@"tmpDic:%@",tmpDic);
                NSString *title = [tmpDic objectForKey:@"title"];
                if ([title isEqualToString:@"账户余额"]||[title isEqualToString:@"充值"]||[title isEqualToString:@"提现"]) {
                    
                }else{
                   
                    [self.dataSource addObject:title];
                }
            }
//            [self.dataSource addObject:@"我的云盒"];
            
            
            [self.tbView reloadData];
            
        }else{
            
        }
    }];
    
}


@end
